// pages/goodsorder/goodsorder.js
const app = getApp();
import request from "../../utils/request";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    orderList: [],
    pageno: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getdataList();
  },
  async getdataList() {
    const res = await request({
      url:
        app.globalData.baseURL +
        `/app-api/package/trade-order/page?pageNo=${1}&pageSize=${50}`,
      method: "GET",
    });
    const list = res.data.list;
    let datalist = list.map((item) => {
      item.payPrice = (item.payPrice / 100).toFixed(2);
      item.expireTimeFormatted = item.expireTime - Date.now();
      if (item.expireTime > Date.now()) {
        item.onShow = true;
      } else {
        item.onShow = false;
      }
      return item;
    });
    this.setData({
      orderList: datalist,
    });
  },
  goback() {
    wx.navigateBack({
      delta: 1,
    });
  },
  async cancelPay(event) {
    const id = event.currentTarget.dataset.item.cartNo;
    wx.showModal({
      title: "提示",
      content: "您确定要将该订单加入配置清单吗？",
      cancelText: "确定",
      confirmText: "取消",
      cancelColor: "#ff0000",
      complete: (res) => {
        if (res.cancel) {
          this.gocancel(id);
        }
        if (res.confirm) {
          console.log("用户点击取消");
        }
      },
    });
  },
  async gocancel(id) {
    const res = await request({
      url:
        app.globalData.baseURL +
        `/app-api/package/trade-order/detail?cartNo=${id}`,
      method: "GET",
    });
    const list = res.data;
    this.creatOrder(list);
  },
  async creatOrder(list) {
    const res = await request({
      url: app.globalData.baseURL + "/app-api/package/cart/create",
      method: "POST",
      data: {
        mainItem: true,
        itemId: list.itemId,
        years: list.years,
      },
    });
    if (res.code === 0) {
      wx.switchTab({
        url: "/pages/order/order",
      });
    } else {
      wx.showToast({
        title: res.msg,
        icon: "none",
      });
    }
  },
  async deliteOrder(event){
    const id = event.currentTarget.dataset.item.id
    wx.showModal({
      title: "提示",
      content: "您确定要删除该订单吗？",
      cancelText: "确定",
      confirmText: "取消",
      cancelColor: "#ff0000",
      complete: (res) => {
        if (res.cancel) {
          this.delitePage(id)
        }
        if (res.confirm) {
          console.log("用户点击取消");
        }
      },
    });
  },
  async delitePage(id){
    const res = await request({
      url: app.globalData.baseURL + `/app-api/package/trade-order/delete?id=${id}`,
      method:'GET'
    })
    if(res.code === 0){
      wx.showToast({
        title: '删除成功',
        icon: 'successs'
      })
      this.getdataList();
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},
  async onScrollToLower() {
    this.setData({
      pageno: this.data.pageno + 1,
    });
    const res = await request({
      url:
        app.globalData.baseURL +
        `/app-api/package/trade-order/page?pageNo=${
          this.data.pageno
        }&pageSize=${50}`,
      method: "GET",
    });
    const list = res.data.list;
    let datalist = list.map((item) => {
      item.payPrice = (item.payPrice / 100).toFixed(2);
      return item;
    });
    this.setData({
      orderList: this.data.orderList.concat(datalist),
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    return {
      title: "嘉之勤官网小程序",
      path: "/pages/index/index",
      imageUrl:
        "https://youyougo2.oss-cn-chengdu.aliyuncs.com/0201506f6fc0f4145e34159c2e2ac66b94b96450dd29e5a19d177edc5dad8023.png",
    };
  },
});
